package com.NIO;

import org.junit.Test;

import java.io.File;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * note:
 * Created by shaxj-mac
 * Date: 16/7/15
 * Time: 下午2:24
 */
public class NIOStudy {

    @Test
    public void MyNIO() throws Exception{
        RandomAccessFile aFile = new RandomAccessFile("src/test/java/com/test.txt", "rw");
        FileChannel inChannel = aFile.getChannel();

        ByteBuffer buf = ByteBuffer.allocate(1000);

        int bytesRead = inChannel.read(buf);
        int cycleTime=0;
        while (bytesRead != -1) {
            cycleTime++;
            System.out.println("Read " + bytesRead);
            buf.flip();

            while(buf.hasRemaining()){
                System.out.print((char) buf.get());
            }

            buf.clear();
            bytesRead = inChannel.read(buf);
        }
        aFile.close();

        System.out.println("cycleTime = " + cycleTime);


    }


}
